﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.Mathematics
{
	public static class VectorMath
	{
		public static Coordinate NormalToTriangle(Coordinate p0, Coordinate p1, Coordinate p2)
		{
			Coordinate v = new Coordinate(p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
			Coordinate v2 = new Coordinate(p2.X - p0.X, p2.Y - p0.Y, p2.Z - p0.Z);
			Coordinate coordinate = VectorMath.CrossProduct(v, v2);
			VectorMath.Normalize(coordinate);
			return coordinate;
		}

		public static void Normalize(Coordinate v)
		{
			double num = System.Math.Sqrt(v.X * v.X + v.Y * v.Y + v.Z * v.Z);
			v.X /= num;
			v.Y /= num;
			v.Z /= num;
		}

		public static Coordinate CrossProduct(Coordinate v1, Coordinate v2)
		{
			double x = VectorMath.Det(v1.Y, v1.Z, v2.Y, v2.Z);
			double y = -VectorMath.Det(v1.X, v1.Z, v2.X, v2.Z);
			double z = VectorMath.Det(v1.X, v1.Y, v2.X, v2.Y);
			return new Coordinate(x, y, z);
		}

		public static double DotProduct(Coordinate v1, Coordinate v2)
		{
			return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
		}

		public static double Det(double a1, double a2, double b1, double b2)
		{
			return a1 * b2 - a2 * b1;
		}
	}
}
